package com.ecocent.common.util;

import java.util.List;
import java.util.Map;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import net.sf.json.processors.JsonValueProcessor;
import net.sf.json.processors.PropertyNameProcessor;
import net.sf.json.util.PropertyFilter;

public final class JsonUtil {
    public static  ObjectMapper objectMapper = new ObjectMapper();
    static{
		  // 测试的时候用 为了格式化json便于查看
    	objectMapper.configure(SerializationFeature.INDENT_OUTPUT, Boolean.TRUE);
    	//objectMapper.setSerializationInclusion(Include.NON_NULL);
		  //如果json有多余的字段，而bean没有相应的属性，忽略报错
    	//objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    }
	public static String getJsonStringFromObject(Object value) throws Exception{
		return objectMapper.writeValueAsString(value);
	}
	public static Object getObjectFromJson(String jsonString,Class<?> classname) throws Exception{
		return objectMapper.readValue(jsonString, classname);
	}
	public static JavaType getCollectionType(Class<?> collectionClass,Class<?>... elementClasses) {
		return objectMapper.getTypeFactory().constructParametricType(
				collectionClass, elementClasses);
	}
	public static Object getCollection(String json, Class<?> collectionClass,Class<?>... elementClasses) throws Exception{
	    JavaType javaType = JsonUtil.getCollectionType(collectionClass,elementClasses); 
		return objectMapper.readValue(json, javaType);
	}
	public static String list2JsonString(List list,String[] includeNames,Class<?> classs,final Map<String,String> reDefineAttributeNames){
		JsonConfig config = new JsonConfig();
		config.registerJsonValueProcessor(java.util.Date.class, new JsonDateValueProcessor());
		final String[] ns = includeNames;
        config.setJsonPropertyFilter(new PropertyFilter() {
            public boolean apply(Object source, String name, Object value) {
                // 只转换我要的属性
	        	if (value == null){ 
	        		return true;
	        	}
            	for(String includeName:ns){
            		if(name.equals(includeName)){
            			return false;
            		}
            	}
                return true;
            }
        });
        config.registerJsonPropertyNameProcessor(classs,  
                // 创建内部类修改字段名字的一个接口  
                        new PropertyNameProcessor() {  
                            @Override  
                            public String processPropertyName(Class obj, String name) {  
                                // 将javaben中的指定字段修改名字,  
                                String rename = reDefineAttributeNames.get(name);
                                if(rename!=null){
                                	return rename;
                                }
                                return name;  
                            }  
                        });  
        JSONArray o = JSONArray.fromObject(list,config);
		return o.toString();
	}
	public static String list2JsonString(List list,String[] includeNames){
		JsonConfig config = new JsonConfig();
		config.registerJsonValueProcessor(java.util.Date.class, new JsonDateValueProcessor());
		final String[] ns = includeNames;
        config.setJsonPropertyFilter(new PropertyFilter() {
            public boolean apply(Object source, String name, Object value) {
                // 只转换我要的属性
	        	if (value == null){
	        		return true;
	        	}
            	for(String includeName:ns){
            		if(name.equals(includeName)){
            			return false;
            		}
            	}
                return true;
            }
        });
        JSONArray o = JSONArray.fromObject(list,config);
		return o.toString();
	}
	public static String object2JsonString(Object object,String[] includeNames,JsonValueProcessor dateProcessor){
		JsonConfig config = new JsonConfig();
		config.registerJsonValueProcessor(java.util.Date.class, dateProcessor);
		final String[] ns = includeNames;
        config.setJsonPropertyFilter(new PropertyFilter() {
            public boolean apply(Object source, String name, Object value) {
                // 只转换我要的属性
	        	if (value == null){
	        		return true;
	        	}
            	for(String includeName:ns){
            		if(name.equals(includeName)){
            			return false;
            		}
            	}
                return true;
            }
        });
        JSONObject o = JSONObject.fromObject(object,config);
		return o.toString();
	}
	public static String object2JsonString(Object object,String[] includeNames){
		JsonConfig config = new JsonConfig();
		config.registerJsonValueProcessor(java.util.Date.class, new JsonDateValueProcessor());
		final String[] ns = includeNames;
        config.setJsonPropertyFilter(new PropertyFilter() {
            public boolean apply(Object source, String name, Object value) {
                // 只转换我要的属性
	        	if (value == null){
	        		return true;
	        	}
            	for(String includeName:ns){
            		if(name.equals(includeName)){
            			return false;
            		}
            	}
                return true;
            }
        });
        JSONObject o = JSONObject.fromObject(object,config);
		return o.toString();
	}
	public static String object2JsonString(Object object){
		JsonConfig config = new JsonConfig();
		config.registerJsonValueProcessor(java.util.Date.class, new JsonDateValueProcessor());
        config.setJsonPropertyFilter(new PropertyFilter() {
            public boolean apply(Object source, String name, Object value) {
                // 只转换我要的属性
	        	if (value == null){
	        		return true;
	        	}
                return false;
            }
        });
        JSONObject o = JSONObject.fromObject(object,config);
		return o.toString();
	}
}
